<?php

/**
 * Created by PhpStorm.
 * User: wen
 * Date: 2017/4/27
 * Time: 18:16
 */
class C_postpay
{
    private $resource;

    private $call_items;

    private $back_items;

    private $resource_id;

    private $payload;

    public function __construct()
    {
        $this->resource = 'C_Order';

        $this->call_items = array(
            "t" => time(),
            "k" => "",
            'uid' => '',
            'v' => API_VERSION,
            "enterpriseId" => "",
            "channelId" => "",
            "userType" => ""
        );

        $this->back_items = array();
    }

    public function set_call_items($obj = array())
    {
        foreach ($this->call_items as $key => $val) {
            if (isset($obj[$key])) {
                $this->call_items[$key] = $obj[$key];
            }
        }
        $this->payload = base64_encode('uid='.$this->call_items['uid'].'&actionType=postPay&platform=WX&channelId='.$this->call_items['channelId'].'&v='.$this->call_items['v'].'&enterpriseId='.$this->call_items['enterpriseId']);
        $this->call_items['k'] = md5($obj['password'].$obj['order_id'] . $this->call_items['t'] . $this->payload);
        $this ->set_resource_id($obj['order_id']);
        return $this->_get_url();
    }

    public function set_back_items($data = array())
    {
    }

    public function set_resource_id($id)
    {
        $this->resource_id = $id;
        return $this->_get_url();
    }

    public function get_call_items()
    {
        return 'payload=' . $this->payload;
    }

    public function get_back_items()
    {
        return $this->back_items;
    }

    public function get_resource_id()
    {
        return $this->resource_id;
    }

    private function _get_url()
    {
        $query_arr = array();
        foreach ($this->call_items as $key => $val) {
            $query_arr[] = $key . '=' . $val;
        }
        if ($this->resource_id) {
            return $this->resource . '/' . $this->resource_id . '/?' . implode('&', $query_arr);
        } else {
            return $this->resource . '/?' . implode('&', $query_arr);
        }
    }
}